package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.io.FileUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.object.pojo.dto.*;
import com.tbit.uqbike.object.pojo.vo.DepositDetailVO;
import com.tbit.uqbike.object.pojo.vo.DepositIncomeVO;
import com.tbit.uqbike.object.pojo.vo.DepositTotalDetailVO;
import com.tbit.uqbike.object.pojo.vo.DepositTotalIncomeVO;
import com.tbit.common.entity.view.PageResult;
import com.tbit.uqbike.service.business.AccountDayStatService;
import com.tbit.uqbike.service.business.DepositStatService;
import com.tbit.uqbike.webmanager.dao.core.AccountDao;
import com.tbit.uqbike.webmanager.dao.log.TransactionLogDao;
import com.tbit.uqbike.webmanager.dao.stat.AccountDayStatDao;
import com.tbit.uqbike.webmanager.util.BeanUtils;
import com.tbit.uqbike.webmanager.util.DateUtils;
import com.tbit.uqbike.webmanager.util.StringUtil;
import com.tbit.uqbike.webmanager.util.csv.CsvExportUtils;
import com.tbit.uqbike.webmanager.util.csv.entity.CsvBaseEntity;
import com.tbit.uqbike.webmanager.util.csv.entity.ExportHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 描述
 *
 * @AUTHOR: wei.xing
 * @DATE: 2022-11-11 14:28
 **/
@Service
public class DepositStatServiceImpl implements DepositStatService {

    @Autowired
    CsvExportUtils csvExportUtils;

    @Resource
    TransactionLogDao transactionLogDao;

    @Autowired
    AccountDayStatService accountDayStatService;


    @Autowired
    AccountDayStatDao accountDayStatDao;

    @Resource
    AccountDao accountDao;

    @Override
    public Long getLastTime(List<Integer> accountIds) {
        return accountDayStatService.selectLatelyDataTime(accountIds);
    }

    @Override
    public DepositTotalDetailVO getTotalDetail(StatDetailDTO dto) {
        return accountDayStatDao.getDepositTotalDetail(
                DateUtils.toYYYYMMDDInt(dto.getStartTime()),
                DateUtils.toYYYYMMDDInt(dto.getEndTime()),
                StringUtil.getIntegerList(dto.getAccountIds()),
                dto.getBrandId());
    }

    @Override
    public PageResult<DepositDetailVO> getDetailPage(StatDetailPageDTO dto) {
        PageHelper.startPage(dto.getPageNO(), dto.getRowCount());
        StatDetailDTO statDetailDTO = BeanUtils.copyProperties(dto, StatDetailDTO.class);
        List<DepositDetailVO> list = getDetailList(statDetailDTO);
        PageInfo<DepositDetailVO> pageInfo = new PageInfo<>(list);
        return new PageResult<>(dto.getPageNO(), dto.getRowCount(), pageInfo.getTotal(), pageInfo.getPages(), list);
    }


    private List<DepositDetailVO> getDetailList(StatDetailDTO dto) {
        List<DepositDetailVO> list = accountDayStatDao.getDepositDetailList(
                DateUtils.toYYYYMMDDInt(dto.getStartTime()),
                DateUtils.toYYYYMMDDInt(dto.getEndTime()),
                StringUtil.getIntegerList(dto.getAccountIds()),
                dto.getBrandId());

        Map<Integer, String> accountMap = accountDao.getByAccountIds(StringUtil.getIntegerList(dto.getAccountIds()))
                .stream().collect(Collectors.toMap(Account::getAccountId, Account::getName));
        list.forEach(v -> v.setAccountName(Integer.valueOf(-1).equals(v.getAccountId()) ? "未知区域" : accountMap.get(v.getAccountId())));
        return list;
    }

    @Override
    public String exportDetail(StatDetailDownloadDTO dto) {
        StatDetailDTO statDetailDTO = BeanUtils.copyProperties(dto, StatDetailDTO.class);
        List<DepositDetailVO> result = getDetailList(statDetailDTO);
        //获取临时目录
        File tempFile = csvExportUtils.getTempFile();
        try {
            File file = csvExportUtils.exportCsv(DepositDetailVO.class, tempFile, dto.getFileName(), result);
            String uploadFileName = UUID.randomUUID().toString().replaceAll("-", "");
            //压缩文件并上传oss
            String url = csvExportUtils.uploadZip(tempFile, uploadFileName, file);
            return url;
        } finally {
            //删除本地文件
            FileUtil.del(tempFile);
        }
    }


    @Override
    public DepositTotalIncomeVO getTotalIncome(DepositIncomeDTO dto) {
        return transactionLogDao.getDepositTotalIncome(
                dto.getBrandId(),
                StringUtil.getIntegerList(dto.getAccountIds()),
                dto.getPhone(),
                dto.getPayPlatform(),
                dto.getIncomeType(),
                dto.getStartTime(),
                dto.getEndTime());

    }

    @Override
    public PageResult<DepositIncomeVO> getIncomeDetailPage(DepositIncomePageDTO dto) {
        PageHelper.startPage(dto.getPageNO(), dto.getRowCount());
        List<DepositIncomeVO> list = transactionLogDao.getDepositIncomeList(
                dto.getBrandId(),
                StringUtil.getIntegerList(dto.getAccountIds()),
                dto.getPhone(),
                dto.getPayPlatform(),
                dto.getIncomeType(),
                dto.getStartTime(),
                dto.getEndTime());
        PageInfo<DepositIncomeVO> pageInfo = new PageInfo(list);
        return new PageResult<>(dto.getPageNO(), dto.getRowCount(), pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getList());
    }

    @Override
    public String exportIncome(DepositIncomeDownloadDTO dto) {
        //获取临时路径
        File tempFile = csvExportUtils.getTempFile();
        try {
            String uploadFileName = UUID.randomUUID().toString().replaceAll("-", "");
            File file = exportIncome(dto, tempFile);
            //压缩上传oss
            return csvExportUtils.uploadZip(tempFile, uploadFileName, file);
        } finally {
            //删除本地文件
            FileUtil.del(tempFile);
        }
    }

    public File exportIncome(DepositIncomeDownloadDTO dto, File tempFile) {
        //自定义导出处理器
        ExportHandler exportHandler = new ExportHandler() {
            @Override
            public List handle(CsvBaseEntity baseEntity) {
                List<DepositIncomeVO> list = transactionLogDao.getDepositIncomeList(
                        dto.getBrandId(),
                        StringUtil.getIntegerList(dto.getAccountIds()),
                        dto.getPhone(),
                        dto.getPayPlatform(),
                        dto.getIncomeType(),
                        baseEntity.getStartTime(),
                        baseEntity.getEndTime());
                return list;
            }
        };

        Date startTime = dto.getStartTime();
        Date endTime = dto.getEndTime();
        //多线程导出csv
        return csvExportUtils.exportBigCsv(exportHandler, DepositIncomeVO.class, startTime, endTime, tempFile, dto.getFileName());
    }
}
